package com.woniuxy.yygh.user.api;

import com.woniuxy.yygh.common.result.Result;
import com.woniuxy.yygh.common.utils.AuthContextHolder;
import com.woniuxy.yygh.model.user.UserInfo;
import com.woniuxy.yygh.user.service.UserInfoService;
import com.woniuxy.yygh.vo.user.LoginVo;
import com.woniuxy.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO: 用户信息用户controoler
 * <P> Date: 2022/3/3:18:52
 * <P> Description:
 */
@Api("用户信息用户controoler")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
        * TODO: 用户手机号登录接口  并返回登录后的信息 生成token
    **/
    @ApiOperation(value = "用户手机号登录接口  并返回登录后的信息")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo){
         Map<String,Object> info =userInfoService.loginUser(loginVo);
         return Result.ok(info);
    }

    /**
        * TODO:用户认证接口 认证数据封装到vo对象
    **/
    @PostMapping("/auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo,
                           HttpServletRequest request){
        //用户id 认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();
    }

    /**
     * TODO:获取用户id信息接口
     **/
    @GetMapping("/auth/getUserInfo")
    public Result getUserinfo(HttpServletRequest request){
        //用户id 认证数据vo对象
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

    /**
        * TODO:通过id获得用户信息 更改密码
    **/
    @PostMapping("/auth/updatePassword")
    public Result updatePassword(HttpServletRequest request,
                                 @RequestBody UserInfo  userInfo){
        Long userId = AuthContextHolder.getUserId(request);

        boolean isOk = userInfoService.updatePassword(userId,userInfo);

        if(isOk){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }


}
